package pattern.behavioral.command;

interface Command {
  void execute();
}

class ConcreteCommand implements Command {
  private Receiver receiver = null;

  ConcreteCommand(Receiver receiver) {
    this.receiver = receiver;
  }

  public void execute() {
    receiver.action();
  }
}


class Receiver {
  //真正执行命令操作的功能代码
  public void action() {
    System.out.println("receiver action.");
  }
}


class Invoker {
  private Command command = null;

  public void setCommand(Command command) {
    this.command = command;
  }

  public void runCommand() {
    command.execute();
  }
}

public class Client {
  public void assemble() {
    //创建接收者
    Receiver receiver = new Receiver();
    //创建命令对象，设定它的接收者
    Command command = new ConcreteCommand(receiver);
    //创建Invoker，把命令对象设置进去
    Invoker invoker = new Invoker();
    invoker.setCommand(command);
    invoker.runCommand();
  }

  public static void main(String[] args) {
    new Client().assemble();
  }
}
